package com.jinbo.footmessageworker.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;


import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.MyApplication;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.httpbean.ProjectBean;
import com.jinbo.footmessageworker.httpbean.WorkerInformationBean;
import com.jinbo.footmessageworker.httpretrofit.BusinessController;
import com.jinbo.footmessageworker.httpretrofit.Listener;
import com.jinbo.footmessageworker.utils.LocalPreferencesHelper;
import com.jinbo.footmessageworker.view.TitleBarView;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class MyProjectsActivity extends BaseActivity implements View.OnClickListener {

    //请求码
    private static final int REQUEST_UPDATE = 100;

    private ListView list;
    List<ProjectBean> projects;
    ArrayList<Integer> ids = new ArrayList<Integer>();
    ServerItemListViewAdapter adapter;

    LinearLayout mTitleLl;

    public static void launch(Activity activity) {//,int requestCode) {
        Intent intent = new Intent(activity, MyProjectsActivity.class);
        activity.startActivity(intent);//.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.right_next_text_layout:
                SelectServeItemActivity.launch(this, ids, REQUEST_UPDATE);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_projects);
        initTitleBarView();
        initViews();
        initData();
    }

    public void initTitleBarView() {
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("我的项目");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        titleBarView.setRightTextIsVisible(true, "修改");
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_rightNextTextLayout, this);
    }

    private void initViews() {
        mTitleLl = (LinearLayout) findViewById(R.id.title);
        list = (ListView) findViewById(R.id.list);
        adapter = new ServerItemListViewAdapter(this, projects);
        list.setAdapter(adapter);
    }

    private void initData() {
        String user_id = MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        BusinessController.getInstance().getMyProjects(user_id, new Listener<WorkerInformationBean>() {
            @Override
            public void onStart(Object... params) {
                showProgressDialog("请稍后...");
            }

            @Override
            public void onComplete(WorkerInformationBean result, Object... params) {
                dismissProgressDialog();
                projects = result.projects;
                adapter.setprojects(projects);
                adapter.notifyDataSetChanged();
                //Log.d("size",projects.size()+"");
                if (projects.size() == 0)
                    mTitleLl.setVisibility(View.VISIBLE);
                else
                    mTitleLl.setVisibility(View.GONE);
                for (int i = 0; i < projects.size(); i++) {
                    //if(projects.get(i)!=null)
                    ids.add(projects.get(i).id);
                }
            }

            @Override
            public void onFail(final String msg, Object... params) {
                dismissProgressDialog();
                if (TextUtils.isEmpty(msg)) {
                } else {
                    show(msg);
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_UPDATE) {
                initData();
            }

        }
    }


}
